const jwt = require('jsonwebtoken');
 const secretKey = process.env.JWT_SECRET || 'vxdfasmlkhnuirapo'; // 从环境变量获取密钥，如果没有则使用默认值（生产环境必须设置环境变量）

 //生成token
 exports.generateToken = (e) => {
    let payload = { id:e,time:Date.now() };
    let token = jwt.sign(payload, secretKey, { expiresIn: '3d' }); // 设置token过期时间为3天
    return token;
 };

 //解码token
 exports.verifyToken = (token) => {
    try {
        let payload;
       jwt.verify(token, secretKey,(error,result)=>{
            if(error){
               payload = 0;
            }else{
                payload = 1;
            }
        }); // 验证token
        return payload;
    } catch (error) {
        return null; // 验证失败，返回null
    }
 };